// components/audioPlayer/index.js

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    url:String,
    index:Number,
    isPlay:{
      type:Boolean,
      value:false,
      observer:function(newValue, oldValue) {
        console.log(newValue)
        this.setData({
          play:newValue
        })
        const isPlay = newValue
        if (isPlay) {
          this.audioPlay()
        } else {
          this.audioPause()
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: { 
    myaudio:null,
    play: false,
    delay: null,
    totalTime: '00:00',
    currentTime: '00:00',
    prevent: 0,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    togglePlay() {
      const isPlay = !this.data.isPlay
      if (isPlay) {          
        this.audioPlay()
      } else {       
        this.audioPause()
      }
    },
    audioPlay() { 
      this.data.myaudio.src = this.data.url
      this.data.myaudio.autoplay = true
      console.log(this.data.myaudio)
      this.data.myaudio.play()
      //开始监听
      this.data.myaudio.onPlay(() => {
        console.log('开始播放')
        this.triggerEvent('play', this.properties.index)
        setTimeout(() => {
          this.data.myaudio.currentTime
          this.data.myaudio.onTimeUpdate(() => {
            console.log(this.data.myaudio.duration)
            const totalTime = this.formatTime(this.data.myaudio.duration)
            const currentTime = this.formatTime(this.data.myaudio.currentTime)
            const prevent = this.data.myaudio.currentTime / this.data.myaudio.duration * 100
            this.setData({
              totalTime,
              currentTime,
              prevent
            })
          })
        }, 1000)
      })
      this.data.myaudio.onError((res) => {
        console.log(res)
      })
      //结束监听
      this.data.myaudio.onEnded(() => {
        console.log('播放至结束')
        this.setData({
          prevent:0,
          currentTime:'00:00'
        })
        this.triggerEvent('pause',this.properties.index)    
      })
    },
    formatTime(time) {
      var minute = Math.floor(time / 60) % 60;
      var second = Math.floor(time) % 60
      return (minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second)
    },
    audioPause() {
      this.data.myaudio.pause()
      //停止监听
      this.data.myaudio.onPause(() => {
        console.log('暂停播放')
        this.triggerEvent('pause', this.properties.index)
      })
    },
    audioStop() {
      this.data.myaudio.stop()
      //停止监听
      this.data.myaudio.onStop(() => {
        console.log('停止播放')
        this.setData({
          prevent:0,
          currentTime:'00:00'
        })
        this.triggerEvent('pause',this.properties.index)  
      })
    },
    sliderChange(e) {
      console.log(e.detail.value) 
      const second = e.detail.value * this.data.myaudio.duration / 100
      console.log(second)
      const currentTime = this.formatTime(second)
      this.setData({
        currentTime
      })
      this.data.myaudio.seek(second)
    },
  },
  lifetimes: {
    ready() {
      const myaudio = wx.createInnerAudioContext()
      this.setData({
        myaudio
      })
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
      console.log('页面退出，播放停止')
      this.data.myaudio.stop()
    },
  }
})
